import { getUserChannels, getAllChannels } from '@/api/channels'

const state = {
  // 我的频道数据
  channelList: [],
  allChannelList: []
}
const mutations = {
  setChannelList(state, payload) {
    state.channelList = payload
  },
  setAllChannelList(state, payload) {
    state.allChannelList = payload
  },
  addChannelList(state, payload) {
    state.channelList.push(payload)
  },
  delChannelList(state, payload) {
    state.channelList = state.channelList.filter(
      (item) => item.id !== payload.id
    )
  }
}
const getters = {
  // 计算属性 ==> 需要计算出可选频道
  // 公式： 所有频道数据 - 我的频道数据 ==> 可选的
  optionsChannelList(state) {
    // 正确： 来遍历所有的频道数组，过滤出在我的频道里面没有的数据
    return state.allChannelList.filter(
      (item) => !state.channelList.some((channel) => channel.id === item.id)
    )
    // return state.allChannelList.filter((item) => {
    //   const res = state.channelList.some((ite) => ite.id === item.id)
    //   if (res) {
    //     return false
    //   } else {
    //     return true
    //   }
    // })
  }
}
const actions = {
  async actions_getChannelList(context) {
    const res = await getUserChannels()
    console.log(res)
    // this.channelList = res.data.channels
    context.commit('setChannelList', res.data.channels)
  },
  async actions_getAllChannelList(context) {
    const res = await getAllChannels()
    context.commit('setAllChannelList', res.data.channels)
  }
}

export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
